using System;
using System.Drawing;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.ToolWindows
{
	/// <summary>
	/// 
	/// </summary>
	public partial class WoWBenchControl
	{
		/// <summary>
		/// Sets the given text in text box.
		/// </summary>
		/// <param name="text">The new text.</param>
		protected void SetText(string text)
		{
			txtOutput.SelectionStart = woWBenchController.BufferPosition;
			txtOutput.SelectionLength = txtOutput.Lines[txtOutput.Lines.Length - 1].Length;
			txtOutput.SelectedText = string.Empty;
			SetTextInLine(text, txtOutput.Lines.Length - 1);
		}

		/// <summary>
		/// Sets the given text in a specified line of text box.
		/// </summary>
		/// <param name="text">The new text.</param>
		/// <param name="index">The Line number.</param>
		protected void SetTextInLine(string text, int index)
		{
			if (txtOutput.Lines.Length > 0)
			{
				try
				{
					string lastLine = txtOutput.Lines[index];
					//if last line is empty then append text.
					if (string.IsNullOrEmpty(lastLine))
					{
						txtOutput.AppendText(text);
					}
					else
					{
						//Append text after appended text in the last line.
						txtOutput.SelectionStart = woWBenchController.BufferPosition;
						txtOutput.SelectionLength = lastLine.Length;
						txtOutput.SelectedText = text;
					}

					if (!string.IsNullOrEmpty(text))
						WoWBenchController.WowBenchCommand.Add(txtOutput.Lines[index]);
				}
				finally
				{
					txtOutput.SelectionStart = txtOutput.Text.Length;
				}
			}
		}

		/// <summary>
		/// Colorize text in the line at the specified index with the specified color.
		/// </summary>
		/// <param name="index">The index of specified line.</param>
		/// <param name="foreColor">The new color of line.</param>
		/// <param name="text">Text to colorize.</param>
		protected void ColorizeText(int index, string text, Color foreColor)
		{
			if (index > -1 && !String.IsNullOrEmpty(text))
			{
				string lastLine = txtOutput.Lines[index];
				int indexFromLine = txtOutput.GetFirstCharIndexFromLine(index);
				ColorizeText(indexFromLine + lastLine.LastIndexOf(text), text.Length, foreColor);
			}
		}

		/// <summary>
		/// Colorize the line at the specified index with the specified foreColor.
		/// </summary>
		/// <param name="index">The index of specified line.</param>
		/// <param name="foreColor">The new color of line.</param>
		protected void ColorizeText(int index, Color foreColor)
		{
			if (index > -1)
			{
				string lastLine = txtOutput.Lines[index];
				ColorizeText(txtOutput.Text.Length - lastLine.Length, lastLine.Length, foreColor);
			}
		}

		/// <summary>
		/// Colorize the line at the specified index with the specified foreColor.
		/// </summary>
		/// <param name="startIndex">The zero-based starting character position of a substring in this instance.</param>
		/// <param name="length">The number of characters in the substring to be colorized.</param>
		/// <param name="foreColor">The BackColor.</param>
		private void ColorizeText(int startIndex, int length, Color foreColor)
		{
			ColorizeText(startIndex, length, foreColor, Color.GhostWhite);
		}

		/// <summary>
		/// Colorize the line at the specified index with the specified backColor.
		/// </summary>
		/// <param name="startIndex">The zero-based starting character position of a substring in this instance.</param>
		/// <param name="length">The number of characters in the substring to be colorized.</param>
		/// <param name="backColor">The BackColor.</param>
		/// <param name="foreColor">The ForeColor.</param>
		private void ColorizeText(int startIndex, int length, Color foreColor, Color backColor)
		{
			if (startIndex > -1)
			{
				txtOutput.SelectionStart = startIndex;
				txtOutput.SelectionLength = length;
				txtOutput.SelectionBackColor = backColor;
				txtOutput.SelectionColor = foreColor;
			}
		}

		/// <summary>
		/// Indicates that the carret is in the last line.
		/// </summary>
		/// <returns></returns>
		protected bool IsLastLine()
		{
			return txtOutput.SelectionStart >= txtOutput.GetFirstCharIndexFromLine(txtOutput.Lines.Length - 1);
		}

		/// <summary>
		/// Set carrets position after BufferPosition.
		/// </summary>
		protected void SetInputLine()
		{
			if (txtOutput.SelectionStart <= woWBenchController.BufferPosition)
				txtOutput.SelectionStart = txtOutput.Text.Length;
		}
	}
}